package org.wendu.wdoa.security.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wendu.wdoa.common.CurrentUser;
import org.wendu.wdoa.common.OaConstants;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.common.token.TokenUser;
import org.wendu.wdoa.common.token.TokenUtils;
import org.wendu.wdoa.security.dto.MenuDto;
import org.wendu.wdoa.security.service.HomeService;

import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    @GetMapping("/menu")
    public Result<List<MenuDto>> userMenuList(
            @RequestHeader(OaConstants.HEADER_NAME_TOKEN) String token){

        TokenUser tokenUser = TokenUtils.getTokenUser(token);

        List<MenuDto> menuList = homeService.getUserMenuList(tokenUser.getUserId());

        return Result.OK(menuList);

    }

//    @GetMapping("/menu") //@AuthenticationPrincipal 说明方法参数为SpringSecurity的当前用户信息
//    public Result<List<MenuDto>> userMenuList(@AuthenticationPrincipal CurrentUser currentUser){
//        CurrentUser currentUser = (CurrentUser)session.getAttribute(
//                OaConstants.SESSION_ATTR_NAME_CURRENT_USER);
//

//        List<MenuDto> menuList = homeService.getUserMenuList(
//                currentUser
//                        .getUserId());
//
//        return Result.OK(menuList);
//
//    }

    @GetMapping("/current-user")
    public Result<TokenUser> getCurrentUser(
            @RequestHeader(OaConstants.HEADER_NAME_TOKEN) String token){

        TokenUser tokenUser = TokenUtils.getTokenUser(token);
        return Result.OK(tokenUser);
    }


}
